package cloudContact.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mybeans.factory.RollbackException;

import cloudContact.GoogleConstant;
import cloudContact.MyDAOException;
import cloudContact.SecurityManager;
import cloudContact.DAO.*;

import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.util.ServiceException;

/**
 * Servlet implementation class FacebookLoadImage
 */
@WebServlet("/FacebookLoadImage")
public class FacebookLoadImage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FacebookLoadImage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Start FacebookLoadImage...");
		
		try {

			if (!SecurityManager.CheckAuthorization(request)) {
				System.out.println("Error: User is not authorized.");
				return;
			}
			
			// Validate ID parameter
			if (request.getParameter("id") == null || request.getParameter("id").toString().equals("")) {
				System.out.println("Error: Missing ID parameter.");
				return;
			}
			
			

			// Retrieve stored google auth token.
			HttpSession session = request.getSession();

			User user = null;
			try {			
				 user = UserDAO.getInstance().lookup(session.getAttribute("userId").toString());			
			} catch (RollbackException e) {
				e.printStackTrace();
			} catch (MyDAOException e) {
				e.printStackTrace();
			}

			if (user == null) {
				System.out.println("Error: User not found.");
				return;
			}
			
			String id = request.getParameter("id").toString();
			FacebookContactDAO dao = FacebookContactDAO.getInstance(session.getId(), "me", user.getFacebookAccessToken());
			FacebookContact contact = dao.lookup(id);
			
			if (contact == null) {
				System.out.println("Error: Google contact with id:" + id + " not found.");
				return;
			}
			
			byte[] imageContent = null;
			
			imageContent = contact.getImage();
			if (imageContent == null) {
				System.out.println("Error: There is no image of id:" + id);
			}
			
			String imageType = "image/png";
			
			// returning a picture stored in a byte[] (and its type in imageType)
			response.setContentType(imageType);
			OutputStream out = response.getOutputStream();
			if (imageContent != null) {
			out.write(imageContent);}
			out.flush();
			out.close();
			
		} catch (RollbackException e) {
			e.printStackTrace();
		} catch (MyDAOException e) {
			e.printStackTrace();
		} finally {
			System.out.println("Finish FacebookLoadImage.");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
